const path = require('node:path')

module.exports = (codeConfig, utils) => {
  const { listPageApiConfig } = codeConfig.apiConfigStates

  const { properties } = listPageApiConfig.request.schema.jsonSchema
  const itemsStr = []

  for (const [prop, propConfig] of Object.entries(properties)) {
    if (prop === 'pageIndex' || prop === 'pageSize') {
      continue
    }
    itemsStr.push(
      `useInput('${prop}', '${utils.extractText(propConfig.description)}')`
    )
  }

  const { formItem } = require('../hooks/index.cjs')
  const code = `/* eslint-disable @typescript-eslint/no-unused-vars */
    import type { Query } from '../types'
    import type { CsFormPropsItem } from 'csui'
    ${formItem}
    export const useSearch = () => {
      const searchItems: CsFormPropsItem<Query>[] = [
        ${itemsStr.join(',')}
      ]
      return {
        searchItems
      }
    }
  `
  utils.writeFile(
    path.join(codeConfig.modulePath, 'hooks', 'use-search.ts'),
    code
  )
}
